package com.eagle.platform.api.controller;

import com.eagle.common.util.JacksonUtils;
import com.eagle.platform.api.constant.APIResultCodeEnum;
import com.eagle.platform.api.vo.APIResultVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    public <T>T parseParam2Bean(String jsonAsString, Class<T> pojoClass){
        if (StringUtils.isNotBlank(jsonAsString)){
            try {
                return JacksonUtils.fromJson(jsonAsString,pojoClass);
            }catch (Exception e){
                e.printStackTrace();
                LOGGER.error("param to query bean error,{}",jsonAsString);
                return null;
            }
        }else {
            return null;
        }
    }

    public APIResultVO buildSuccessResult(Object data){
        APIResultVO apiResultVO = new APIResultVO();
        apiResultVO.setCode(APIResultCodeEnum.SUCCESS.getCode());
        apiResultVO.setMsg(APIResultCodeEnum.SUCCESS.getDesc());
        apiResultVO.setData(data);
        return apiResultVO;
    }

    public APIResultVO buildApiResult(APIResultCodeEnum codeEnum,Object data){
        APIResultVO apiResultVO = new APIResultVO();
        apiResultVO.setCode(codeEnum.getCode());
        apiResultVO.setMsg(codeEnum.getDesc());
        apiResultVO.setData(data);
        return apiResultVO;
    }

}
